app.controller('articleController', function($scope,$http ,$controller,$sce,$rootScope, articleService) {
	$controller('baseController' , {$scope:$scope});
	
	$scope.status = 0;
	$scope.selectStatus = 0;
	if(localStorage.status){
		$scope.status = localStorage.status;
		$scope.selectStatus = localStorage.status;
	}else{
		location.href="../login.html";
	}
	
    $scope.item2 = true;
    $scope.item3 = true;
    $scope.item4 = true;
    $scope.item1Arr =[];
    $scope.item2Arr =[];
    $scope.item3Arr =[];
    $scope.item4Arr =[];
    $scope.item1Value = 0;
    $scope.item2Value = 0;
    $scope.item3Value = 0;
    $scope.item4Value = 0;
    
    

    $scope.selectId = 0;
    $scope.title = '';
    $scope.videoUrl = $sce.trustAsResourceUrl('');
    
    //信任video
    $scope.trustUrl = function(url){
    	return $sce.trustAsResourceUrl(url);
    }
    
    $scope.schoolList=[];   //读取分驾校名称
    $http.get('../sealSchool/selectSchoolList.do').success(
			function(response){
				$scope.schoolList=response;
				console.log($scope.schoolList);
			}
	);
  //加载层
    var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
    //初始化选择分类  第一个选择框
    $http.get("../article/findcategoryByPid.do?pid="+$scope.item1Value).success(function (response) {
    	console.log(response);
    	$scope.item1Arr = response;
    	//关闭
    	layer.close(index);
    });
    //提取 第二个选择框的内容
    $scope.selectItem2ById = function(){
    	console.log($scope.item1Value,'选择item1');
    	if($scope.item1Value != 0){
    		console.log(angular.isNumber(parseInt($scope.item1Value)))
    		if(angular.isNumber(parseInt($scope.item1Value))){
    			$scope.selectId = $scope.item1Value;
    			$rootScope.searchId = $scope.item1Value;
    		}
    		$http.get("../article/findcategoryByPid.do?pid="+$scope.item1Value).success(function (response) {
    	    	/*console.log(response,response.length);*/
    	    	if(response.length !=0){
    	    		$scope.item2Arr =[];
    	    		$scope.item3Arr =[];
    	    		$scope.item2Arr = response;
    	    		
    	    		console.log($scope.item2Arr,$scope.item3Arr);
        	    	$scope.item2 = false;
        	        $scope.item3 = true;
        	        $scope.item4 = true;
    	    	}else{
    	    		$scope.item2Arr =[];
    	    		$scope.item3Arr =[];
    	    		$scope.item4Arr =[];
        	    	$scope.item2 = true;
        	        $scope.item3 = true;
        	        $scope.item4 = true;
    	    	}    	    	
    	    });
    	}
    }
    
  //提取 第三个选择框的内容
    $scope.selectItem3ById = function(){
    	console.log($scope.item2Value,'选择item2');
    	if($scope.item2Value != 0){
    		console.log(angular.isNumber(parseInt($scope.item2Value)))
    		if(angular.isNumber(parseInt($scope.item2Value))){
    			$scope.selectId = $scope.item2Value;
    			$rootScope.searchId = $scope.item1Value;
    		}
    		$http.get("../article/findcategoryByPid.do?pid="+$scope.item2Value).success(function (response) {
//    	    	console.log(response);
    	    	if(response.length !=0){
    	    		$scope.item3Arr =[];
    	    		$scope.item3Arr = response;
    	    		console.log($scope.item3Arr);
    	    		$scope.item2 = false;
        	        $scope.item3 = false;
        	        $scope.item4 = true;
    	    	}else{
    	    		$scope.item3Arr =[];
    	    		$scope.item4Arr =[];
        	    	$scope.item2 = false;
        	        $scope.item3 = true;
        	        $scope.item4 = true;
    	    	}     	    	
    	    });
    	}
    }
  //提取 第四个选择框的内容
    $scope.selectItem4ById = function(){
    	console.log($scope.item3Value,'选择item3');
    	if($scope.item3Value != 0){
    		console.log(angular.isNumber(parseInt($scope.item3Value)))
    		if(angular.isNumber(parseInt($scope.item3Value))){
    			$scope.selectId = $scope.item3Value;
    			$rootScope.searchId = $scope.item1Value;
    		}
    	}
    }
    //上传视频
    $scope.save = function() { 
        var fd = new FormData();
        var file = document.querySelector('input[type=file]').files[0];
//        var file = $scope.myFile;
        console.log(file);
        fd.append('video', file); 
         $http({
              method:'POST',
              url:"../article/uploadVideo.do",
              data: fd,
              headers: {'Content-Type':undefined},
              transformRequest: angular.identity 
               })   
              .success( function (response){
			      // 上传成功的操作
            	  console.log(response,'视频链接');
            	  $scope.mp4Url = response.videoResult;
            	  $scope.videoUrl = $sce.trustAsResourceUrl(response.videoResult);
              }); 

     }
    //取消设置
    $scope.resetInput = function(){
    	$scope.title = '';
    	$('#suolue1').val('');
    	$('#suolue2').val('');
    	$('#suolue3').val('');
    	$('#time').val('');
    	$('#passnum').val('');
    	$scope.mp4Url = '';
    	$scope.videoUrl = '';
    	$('#contentHtml').val('');
    	editor.txt.html('');
    }
    // 显示细节
    $scope.showDetail = function(index){
//    	alert(index);
    	var item = $scope.newslist[index];
    	$scope.id = item.id;
    	$scope.title = item.title;
    	$('#suolue1').val(item.imgUrl1);
    	$('#suolue2').val(item.imgUrl2);
    	$('#suolue3').val(item.imgUrl3);
    	$('#time').val(item.time);
    	$('#passnum').val(item.passnum);
    	$scope.mp4Url = item.videoUrl;
    	$scope.videoUrl = $scope.trustUrl(item.videoUrl);
    	$('#contentHtml').val(item.content);
    	editor.txt.html(item.content);
    	$scope.selectId = item.classification;
    	$scope.selectStatus = item.status;
    }
    //添加文章 提交
    $scope.submitContent = function(){
    	var news = {};
        news.title = $scope.title;
        news.imgUrl1 = $('#suolue1').val();
        news.imgUrl2 = $('#suolue2').val();
        news.imgUrl3 = $('#suolue3').val();
        news.time = $('#time').val();
        news.passnum = $('#passnum').val();
        news.videoUrl = $scope.mp4Url;
        news.content = $('#contentHtml').val();
        console.log($scope.selectId,'关于驾校');
        news.classification = $scope.selectId;
        news.status = $scope.selectStatus;
        $http.post('../article/addArticle.do',news).success( function (res){
		      // 上传成功的操作
      	  console.log(res,'提交成功');
      	  if(res.success){
      		layer.msg(res.message,function(){
      			self.location.reload();
      		});
      		
      	  }else{
      		layer.msg(res.message); 
      	  }

        }); 
        
        console.log(news,'文章内容');
    }
    
  //修改文章 提交
    $scope.saveContent = function(){
    	var news = {};
    	news.id = $scope.id;
        news.title = $scope.title;
        news.imgUrl1 = $('#suolue1').val();
        news.imgUrl2 = $('#suolue2').val();
        news.imgUrl3 = $('#suolue3').val();
        news.videoUrl = $scope.mp4Url;
        news.time = $('#time').val();
        news.passnum = $('#passnum').val();
        news.content = $('#contentHtml').val();
        news.classification = $scope.selectId;
        news.status = $scope.selectStatus;
        $http.post('../article/saveArticle.do',news).success( function (res){
		      // 上传成功的操作
      	  console.log(res,'修改成功');
      	  if(res.success){
      		layer.msg("修改成功",function(){
      			self.location.reload();
      		});
      		
      	  }else{
      		layer.msg(res.message); 
      	  }

        }); 
        
        console.log(news,'修改文章内容');
    }

    //删除  批量删除  全选
    $scope.allSelection =function($event){
    	if($event.target.checked){
        	var _checkbox = $("tbody").find("[type='checkbox']").not("[disabled]");
        	_checkbox.each(function () {
        		$(this).prop("checked",true);
        		$scope.selectIds.push($(this).attr("id"));
                // 判断是否选中
//               var delFlag = $(this).is(":checked");
//               if (delFlag) {
//            	   $scope.selectIds.push($(this).attr("id"));
//               }
            });    				   					
		 }else{
			 $scope.selectIds = [];
			 var _checkbox = $("tbody").find("[type='checkbox']").not("[disabled]");
        	_checkbox.each(function () {
        		$(this).prop("checked",false);
            });
		}
    	console.log($scope.selectIds,'全选')
    }
    
	
	//条件查询
    $scope.searchByClassId = function(){
    	$scope.classId = $scope.selectId;
    	$scope.search($scope.paginationConf.currentPage, $scope.paginationConf.itemsPerPage);
    }
    $scope.classId=0;
//    $scope.status = 1;
	$scope.search=function(page, rows){
		$http.get('../article/search.do?page='+page+'&rows='+rows+'&classId='+$scope.classId+'&status='+$scope.status).success(
			function(response){
				console.log(response);
				$scope.newslist=response.rows;	//显示当前页数据
				$scope.paginationConf.totalItems=response.total;	//更新总记录数
				
				
			}		
		);
	}

    //确定删除
    $scope.deleteByArr = function(){
    	console.log($scope.selectIds);
    	if($scope.selectIds.length > 0){
    		//询问框
    		layer.confirm('确定要删除？', {
    		  btn: ['确定','取消'] //按钮
    		}, function(){
    	        $http.post('../article/deleteArticleByIdArray.do',$scope.selectIds).success( function (res){
    	      	  console.log(res,'批量刪除');
    	      	  if(res.success){
    	      		layer.msg(res.message,function(){
    	      			self.location.reload();
    	      		},{icon: 1});	
    	      	  }else{
    	      		layer.msg(res.message); 
    	      	  }
    	          }); 
    		  
    		}, function(){
    			layer.msg('已取消删除',{icon:2});
    		});
    	}else{
    		layer.msg('未选择任何选项',{icon:2});
    	}

    }   
});